﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ITAP.ContourBuilder.GUI
{
    public delegate void CutoutParametersChangedDelegate();

    public partial class FormCreateCutout : Form
    {
        private double maximumLength;
        public event CutoutParametersChangedDelegate ParametersChanged;

        public double CutoutStart
        {
            get
            {
                return (double)numericUpDownCutoutStart.Value;
            }
            set
            {
                numericUpDownCutoutStart.Value = (decimal)value;
            }
        }

        public double CutoutLength
        {
            get
            {
                return (double)numericUpDownCutoutLength.Value;
            }
            set
            {
                numericUpDownCutoutLength.Value = (decimal)value;
            }
        }

        public double CutoutDepth
        {
            get
            {
                return (double)numericUpDownCutoutDepth.Value;
            }
            set
            {
                numericUpDownCutoutDepth.Value = (decimal)value;
            }
        }


        private decimal primitiveLength;

        public FormCreateCutout(double primitiveLength)
        {
            InitializeComponent();
            this.primitiveLength = (decimal)primitiveLength;
            numericUpDownCutoutLength.Maximum = (decimal)primitiveLength;

        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void numericUpDownCutoutStart_ValueChanged(object sender, EventArgs e)
        {
            this.UpdateState();
        }

        private void numericUpDownCutoutLength_ValueChanged(object sender, EventArgs e)
        {
            this.UpdateState();
        }

        private void numericUpDownCutoutDepth_ValueChanged(object sender, EventArgs e)
        {
            this.UpdateState();
        }

        private void numericUpDownCutoutStart_KeyDown(object sender, KeyEventArgs e)
        {
            this.UpdateState();
        }

        private void numericUpDownCutoutLength_KeyDown(object sender, KeyEventArgs e)
        {
            this.UpdateState();
        }

        private void numericUpDownCutoutDepth_KeyDown(object sender, KeyEventArgs e)
        {
            this.UpdateState();
        }

        private void UpdateState()
        {
            this.numericUpDownCutoutStart.Maximum = primitiveLength - numericUpDownCutoutLength.Value;
            this.numericUpDownCutoutLength.Maximum = primitiveLength - numericUpDownCutoutStart.Value;
            if (this.ParametersChanged != null)
                this.ParametersChanged.Invoke();
        }

        private void numericUpDownCutoutStart_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.UpdateState();
        }

        private void numericUpDownCutoutLength_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.UpdateState();
        }

        private void numericUpDownCutoutDepth_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.UpdateState();
        }
    }
}
